package com.example.auth.utils;

import cn.hutool.core.util.ArrayUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrUtils {

    /**
     * 把逗号分隔的字符串转换字符串数组
     *
     * @param str
     * @return
     */
    public static String[] splitStr2StrArr(String str,String split) {
        if (str != null && !str.equals("")) {
            return str.split(split);
        }
        return null;
    }


    /**
     * 把逗号分隔字符串转换List的Long
     *
     * @param str
     * @return
     */
    public static List<Long> splitStr2LongArr(String str) {
        String[] strings = splitStr2StrArr(str,",");
        if (strings == null) return null;

        List<Long> result = new ArrayList<>();
        for (String string : strings) {
            result.add(Long.parseLong(string));
        }

        return result;
    }
    /**
     * 把逗号分隔字符串转换List的Long
     *
     * @param str
     * @return
     */
    public static List<Long> splitStr2LongArr(String str,String split) {
        String[] strings = splitStr2StrArr(str,split);
        if (strings == null) return null;

        List<Long> result = new ArrayList<>();
        for (String string : strings) {
            result.add(Long.parseLong(string));
        }

        return result;
    }

    public static String getRandomString(int length) {
        String str = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(10);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static String getComplexRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static String convertPropertiesToHtml(String properties){
        //1:容量:6:32GB_4:样式:12:塑料壳
        StringBuilder sBuilder = new StringBuilder();
        String[] propArr = properties.split("_");
        for (String props : propArr) {
            String[] valueArr = props.split(":");
            sBuilder.append(valueArr[1]).append(":").append(valueArr[3]).append("<br>");
        }
        return sBuilder.toString();
    }

    /**
     * 字符串是否包含空格
     * @param strs
     * @return
     */
    public static boolean hasBlank(String... strs) {
        if (ArrayUtil.isEmpty(strs)) {
            return true;
        }

        for (String str : strs) {
            if (str.contains(" ")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否只包含中文
     * @param strs
     * @return
     */
    public static boolean chineseOnly(String... strs) {
        if (ArrayUtil.isEmpty(strs)) {
            return true;
        }

        for (String str : strs) {
            String regEx="^[\\u4e00-\\u9fa5]*$";
            Pattern p=Pattern.compile(regEx);
            Matcher matcher = p.matcher(str);
            return matcher.matches();
        }
        return false;
    }

    /**
     * 是否只包含中文汉字数字
     * @param strs
     * @return
     */
    public static boolean wordNumberOnly(String... strs) {
        if (ArrayUtil.isEmpty(strs)) {
            return true;
        }

        for (String str : strs) {
            String regEx="^[A-z0-9\\u4e00-\\u9fa5]*$";
            Pattern p=Pattern.compile(regEx);
            Matcher matcher = p.matcher(str);
            return matcher.matches();
        }
        return false;
    }

    /**
     * 是否只包含中文汉字数字括号
     * @param strs
     * @return
     */
    public static boolean wordNumberBracketsOnly(String... strs) {
        if (ArrayUtil.isEmpty(strs)) {
            return true;
        }

        for (String str : strs) {
            String regEx="^[A-z0-9\\(\\)\\（\\）\\u4e00-\\u9fa5]*$";
            Pattern p=Pattern.compile(regEx);
            Matcher matcher = p.matcher(str);
            return matcher.matches();
        }
        return false;
    }

    /**
     * 是否只包含中文汉字括号
     * @param strs
     * @return
     */
    public static boolean wordBracketsOnly(String... strs) {
        if (ArrayUtil.isEmpty(strs)) {
            return true;
        }

        for (String str : strs) {
            String regEx="^[\\(\\)\\（\\）\\u4e00-\\u9fa5]*$";
            Pattern p=Pattern.compile(regEx);
            Matcher matcher = p.matcher(str);
            return matcher.matches();
        }
        return false;
    }

    /**
     * 是否只包含中文汉字数字
     * @param strs
     * @return
     */
    public static boolean numberOnly(String... strs) {
        if (ArrayUtil.isEmpty(strs)) {
            return true;
        }

        for (String str : strs) {
            String regEx="^[0-9]*$";
            Pattern p=Pattern.compile(regEx);
            Matcher matcher = p.matcher(str);
            return matcher.matches();
        }
        return false;
    }

    /**
     * 字符串是否包含下划线
     * @param strs
     * @return
     */
    public static boolean hasUnderline(String... strs) {
        if (ArrayUtil.isEmpty(strs)) {
            return true;
        }
        for (String str : strs) {
            if (str.contains("_")) {
                return true;
            }
        }
        return false;
    }

}
